<?php

/*
	info: 分类管理
	date: 2011-11-23
*/

if (!defined('ADMIN_WEBINROOT'))
{
	header("HTTP/1.0 404 Not Found");
	exit();
}

require WEBFILEPATH.'/include/category.class.php';

$args = array(
	'cache' => true
);

$webrequest['type'] = isset($webrequest['type']) ? trim($webrequest['type']) : '';

if (!$webrequest['type']) {
	admin_ShowMessage('参数错误,请返回确认！', '', 'BACK');
}
$categoryDataModel = CategoryDataModel::factory($webrequest['type'], $args);

$webglobal['file_category'] = array();
$webglobal['file_category']['type'] = $webrequest['type'];
$webglobal['file_category']['title'] = $categoryDataModel->getTitle();

if ($webrequest['type'] == 'area') {
	$webglobal['file_category']['leveloption'] = $categoryDataModel->areaLevelTypeMap();
}

if ($webrequest['type'] == 'news') {
	$webglobal['file_category']['channeltype'] = array(
		1 => '普通文章',
		2 => '图片集',
	);
}
//添加分类
if ($webglobal['mode'] == 'categoryadd') {
	if (global_CheckSubmit('adminsubmit')) {
		
		$webrequest['upid'] = isset($webrequest['upid']) ? trim($webrequest['upid']) : 0;
		$webrequest['categoryname'] = isset($webrequest['categoryname']) ? trim($webrequest['categoryname']) : '';
		$webrequest['categorysort'] = isset($webrequest['categorysort']) ? intval($webrequest['categorysort']) : 0;

		if ($webrequest['upid'] && !$categoryDataModel->getItem($webrequest['upid'])) {
			admin_ShowMessage('上级分类不存在,请返回确认！', '', 'BACK');
		}

		if ($webrequest['categoryname'] == '') {
			admin_ShowMessage('分类名称不能为空！', '', 'BACK');
		}

		$tempInsertData = array(
			'upid' => $webrequest['upid'],
			'name' => $webrequest['categoryname'],
			'sort' => $webrequest['categorysort']
		);

		if ($webrequest['type'] == 'area') {
			$webrequest['categoryshort'] = isset($webrequest['categoryshort']) ? trim($webrequest['categoryshort']) : '';
			$webrequest['categorylevel'] = isset($webrequest['categorylevel']) ? intval($webrequest['categorylevel']) : '';
			if (!preg_match("/^[0-9a-z_]+$/", $webrequest['categoryshort'])) {
				admin_ShowMessage('字母缩写不能空或者格式错误, 请返回重新填写！', '', 'BACK');
			}

			if ($shortData = $categoryDataModel->getItem($webrequest['categoryshort'], 'sign')) {
				admin_ShowMessage('系统中已经存在该字母缩写，请返回重新填写！', '', 'BACK');
			}
			$tempInsertData['short_english'] = $webrequest['categoryshort'];
			$tempInsertData['level'] = $webrequest['categorylevel'];
		}

		if ($webrequest['type'] == 'news') {
			$tempInsertData['channeltype'] = isset($webrequest['channeltype']) ? intval($webrequest['channeltype']) : 1;
			$tempInsertData['ispart'] = isset($webrequest['ispart']) ? trim($webrequest['ispart']) : 0;
			$tempInsertData['seotitle'] = isset($webrequest['seotitle']) ? trim($webrequest['seotitle']) : '';
			$tempInsertData['keywords'] = isset($webrequest['keywords']) ? trim($webrequest['keywords']) : '';
			$tempInsertData['description'] = isset($webrequest['description']) ? trim($webrequest['description']) : '';
		}
		admin_CheckFromForm(ADMIN_WEBURLPATH."/index.php?mode=category&type=".$webrequest['type']);
		if (!($insertID = $categoryDataModel->addData($tempInsertData))) {
			admin_ShowMessage('数据添加失败，请联系管理员！', '', 'BACK');
		}

		admin_AddLog("添加". $categoryDataModel->getTitle() ."分类|id: {$insertID}; name: {$webrequest['categoryname']}");
		admin_ShowMessage($categoryDataModel->getTitle(). '分类添加成功。', ADMIN_WEBURLPATH."/index.php?mode=category&type=".$webrequest['type']);
	}
	else {
		$webglobal['file_category']['categoryoption'] = $categoryDataModel->formatCategoryOption(0, 0);
		$webglobal['file_category']['relid'] = isset($webrequest['relid']) ? trim($webrequest['relid']) : 0;
		admin_StartFromForm();
	}

} elseif ($webglobal['mode']=='categoryedit') {
	$webrequest['editid'] = isset($webrequest['editid']) ? intval($webrequest['editid']) : 0;

	if ($webrequest['editid'] <= 0) {
		admin_ShowMessage('不存在该分类。', '', 'BACK');
	}

	if (!$webglobal['file_category']['editinfo'] = $categoryDataModel->getItem($webrequest['editid'])) {
		admin_ShowMessage('不存在该分类。', '', 'BACK');
	}

	if (global_CheckSubmit('adminsubmit')) {
		$webrequest['upid'] = isset($webrequest['upid']) ? trim($webrequest['upid']) : 0;
		$webrequest['categoryname'] = isset($webrequest['categoryname']) ? trim($webrequest['categoryname']) : '';
		$webrequest['categorysort'] = isset($webrequest['categorysort']) ? intval($webrequest['categorysort']) : 0;

		if ($webrequest['upid']) {
			if (!$upCategory = $categoryDataModel->getItem($webrequest['upid'])) {
				admin_ShowMessage('上级分类不存在,请返回确认！', '', 'BACK');
			}
			if ($webrequest['editid'] == $webrequest['upid'] || in_array($webrequest['editid'], $upCategory['upCategoryID'])) {
				admin_ShowMessage('该分类不能被修改到它的子分类！', '', 'BACK');
			}
		}

		if ($webrequest['categoryname'] == '') {
			admin_ShowMessage('分类名称不能为空！', '', 'BACK');
		}
		
		$tempUpdateData = array(
			'upid' => $webrequest['upid'],
			'name' => $webrequest['categoryname'],
			'sort' => $webrequest['categorysort']
		);

		if ($webrequest['type'] == 'area') {
			$webrequest['categoryshort'] = isset($webrequest['categoryshort']) ? trim($webrequest['categoryshort']) : '';
			$webrequest['categorylevel'] = isset($webrequest['categorylevel']) ? intval($webrequest['categorylevel']) : '';
			if (!preg_match("/^[0-9a-z_]+$/", $webrequest['categoryshort'])) {
				admin_ShowMessage('字母缩写不能空或者格式错误, 请返回重新填写！', '', 'BACK');
			}

			if ($categoryDataModel->checkSignForEdit($webrequest['categoryshort'], $webrequest['editid'])) {
				admin_ShowMessage('系统中已经存在该字母缩写,请返回确认！', '', 'BACK');
			}

			$tempUpdateData['short_english'] = $webrequest['categoryshort'];
			$tempUpdateData['level'] = $webrequest['categorylevel'];
		}

		if ($webrequest['type'] == 'news') {
			$tempUpdateData['channeltype'] = isset($webrequest['channeltype']) ? intval($webrequest['channeltype']) : 1;
			$tempUpdateData['ispart'] = isset($webrequest['ispart']) ? intval($webrequest['ispart']) : 0;
			$tempUpdateData['seotitle'] = isset($webrequest['seotitle']) ? trim($webrequest['seotitle']) : '';
			$tempUpdateData['keywords'] = isset($webrequest['keywords']) ? trim($webrequest['keywords']) : '';
			$tempUpdateData['description'] = isset($webrequest['description']) ? trim($webrequest['description']) : '';
		}

		admin_CheckFromForm(ADMIN_WEBURLPATH."/index.php?mode=category&type=".$webrequest['type']);

		if (!$tempReturn = $categoryDataModel->editData($webrequest['editid'], $tempUpdateData)) {
			admin_ShowMessage('数据修改失败，请联系管理员！', '', 'BACK');
		}

		admin_AddLog("修改". $categoryDataModel->getTitle() ."分类| id: {$webrequest['editid']}; title: {$webrequest['categoryname']}");
		admin_ShowMessage($categoryDataModel->getTitle() . '分类修改成功。', ADMIN_WEBURLPATH."/index.php?mode=category&type=".$webrequest['type']);
	} else {
		$webglobal['file_category']['categoryoption'] = $categoryDataModel->formatCategoryOption(0, 0);
		foreach ($webglobal['file_category']['categoryoption'] as $id => $category) {
			if ($id == $webrequest['editid'] || in_array($webrequest['editid'], $category['upCategoryID'])) {
				unset($webglobal['file_category']['categoryoption'][$id]);
			}
		}
		admin_StartFromForm();
	}

} elseif ($webglobal['mode']=='categorydel') {
	$webrequest['delid'] = isset($webrequest['delid']) ? intval($webrequest['delid']) : 0;

	if ($webrequest['delid'] <= 0) {
		admin_ShowMessage('不存在该'. $categoryDataModel->getTitle() .'分类。', '', 'BACK');
	}

	if (!$categoryData = $categoryDataModel->getItem($webrequest['delid'])) {
		admin_ShowMessage('不存在该'. $categoryDataModel->getTitle() .'分类。', '', 'BACK');
	}

	if ($tempReturn = $categoryDataModel->deleteData($webrequest['delid'])) {
		if (is_array($tempReturn)) {
			admin_ShowMessage($tempReturn['msg'], '', 'BACK');
		} else {
			admin_AddLog("删除". $categoryDataModel->getTitle() ."分类| id: {$webrequest['delid']}; title: " .$categoryData['name']);
			admin_ShowMessage('分类删除成功。', ADMIN_WEBURLPATH."/index.php?mode=category&type=".$webrequest['type']);
		}
	} else {
		admin_ShowMessage('数据修改删除失败，请联系管理员！', '', 'BACK');
	}

//分类列表
} elseif ($webglobal['mode']=='category') {
	$webglobal['file_category']['dataarray'] = $categoryDataModel->formatCategoryOption(0, 0);
}

if ($webrequest['type'] == 'news') {
	template_Display('category_news', 1);
} else {
	template_Display('category', 1);
}